//
//  MusicViewController.m
//  Crocodile
//
//  Created by tin vu on 8/29/12.
//  Copyright (c) 2012 hust. All rights reserved.
//

#import "MusicViewController.h"
#import "Constant.h"
#import "CarouselItem.h"
#import "TabbarViewController.h"
#import "Song.h"
#import "avTouchViewController.h"

@interface MusicViewController () 

@end

@implementation MusicViewController
@synthesize carousel;
@synthesize itemsArr;
@synthesize songsArr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    playScreen = [[avTouchViewController alloc] initWithNibName:@"avTouchViewController" bundle:nil];
    playScreen.hidesBottomBarWhenPushed = YES;
    playScreen.currentSongIndex = -1;
    
	// Do any additional setup after loading the view.
    UIImage *backgroundImg = [UIImage imageNamed:kImgBackground];
    UIImageView *backgroundImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [backgroundImgView setImage:backgroundImg];
    backgroundImgView.alpha = 0.5;
    [self.view addSubview:backgroundImgView];
    [backgroundImg release];
    [backgroundImgView release];
    
    self.view.backgroundColor = [UIColor clearColor];
    
    UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithTitle:kStrBackBtnTitle style:UIBarButtonItemStyleBordered target:self action:@selector(onBack)];
    [self.navigationItem setLeftBarButtonItem:backBtnItem];
    [backBtnItem release];
    
    UIBarButtonItem *goBtnItem = [[UIBarButtonItem alloc] initWithTitle:kStrGoBtnTitle style:UIBarButtonItemStyleBordered target:self action:@selector(onGo)];
    [self.navigationItem setRightBarButtonItem:goBtnItem];
    [goBtnItem release];
    
    itemsArr = [[NSMutableArray alloc] init];
    CarouselItem *tempCarouselItem;
    
    tempCarouselItem = [[CarouselItem alloc] initWithImageName:@"qnga1.jpeg" title:@"Album vol 1"];
    [itemsArr addObject:tempCarouselItem];
    [tempCarouselItem release];
    
    tempCarouselItem = [[CarouselItem alloc] initWithImageName:@"qnga2.jpeg" title:@"Live show tháng 2"];
    [itemsArr addObject:tempCarouselItem];
    [tempCarouselItem release];
    
    tempCarouselItem = [[CarouselItem alloc] initWithImageName:@"qnga3.jpeg" title:@"Quỳnh Nga-Khánh Phương"];
    [itemsArr addObject:tempCarouselItem];
    [tempCarouselItem release];
    
    tempCarouselItem = [[CarouselItem alloc] initWithImageName:@"ngaq1.jpeg" title:@"Album vol 2"];
    [itemsArr addObject:tempCarouselItem];
    [tempCarouselItem release];
    
    tempCarouselItem = [[CarouselItem alloc] initWithImageName:@"ngaq3.jpeg" title:@"Album vol 3"];
    [itemsArr addObject:tempCarouselItem];
    [tempCarouselItem release];
    
    self.navigationItem.title = ((CarouselItem *)[itemsArr objectAtIndex:0]).title;
    
    // Initialize and configure the carousel
	carousel = [[iCarousel alloc] initWithFrame:CGRectMake(0, 50, 320, 160)];
	carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    carousel.type = iCarouselTypeRotary;
	carousel.dataSource = self;
	
	[self.view addSubview:carousel];
    
    // Set data source for table view
    songsArr = [self getSongsArrByCategoryId:0];
    playScreen.songsArr = songsArr;
    
    // Add table view
    UITableView *songTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 170, 320, 200) style:UITableViewStylePlain];
    songTableView.delegate = self;
    songTableView.dataSource = self;
    songTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:songTableView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [super dealloc];
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
	[super loadView];
}

#pragma mark -
#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [itemsArr count];
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if(view == nil)
    {
        CarouselItem *tempCarouselItem = [itemsArr objectAtIndex:index];
        UIImage *image = [UIImage imageNamed:tempCarouselItem.imageName];
        //UIImage *image = [itemsArr objectAtIndex:index];
        UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 125, 160)] autorelease];
        [button setBackgroundImage:image forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        button.tag=index;
        view = button;
    }
    return view;
}

- (void)buttonTapped:(UIButton *)button
{
    CarouselItem *tempCarouselItem = [itemsArr objectAtIndex:button.tag];
    self.navigationItem.title = tempCarouselItem.title;
}

#pragma mark- Actions
- (void)onBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onGo
{
    if(playScreen.currentSongIndex >=0)
    {
        playScreen.isPushFromSelectedRowEvent = NO;
        [self.navigationController pushViewController:playScreen animated:YES];
    }
    else {
        playScreen.currentSongIndex = 0;
        playScreen.isPushFromSelectedRowEvent = YES;
        [self.navigationController pushViewController:playScreen animated:YES];
    }
}

#pragma mark- Table View datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [songsArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Song *tempSong = [songsArr objectAtIndex:[indexPath row]];
    
    UILabel *titleLbl;
    UIButton *playButton;
    
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        playButton = [[UIButton alloc] initWithFrame:CGRectMake(3, 6, 27, 27)];
        playButton.backgroundColor = [UIColor clearColor];
        [playButton setBackgroundImage:[UIImage imageNamed:kImgPlayIcon] forState:UIControlStateNormal];
        [playButton addTarget:nil action:@selector(onPlayButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
        playButton.tag = 100;
        
        titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(40, 3, 280, 30)];
        titleLbl.backgroundColor = [UIColor clearColor];
        titleLbl.textColor = [UIColor whiteColor];
        titleLbl.tag = 101;
        
        [cell addSubview:playButton];
        [cell addSubview:titleLbl];
        
        [playButton release];
        [titleLbl release];
    }
        
    playButton = (UIButton *)[cell viewWithTag:100];
    
    titleLbl = (UILabel *)[cell viewWithTag:101];
    titleLbl.text = tempSong.name;
    
    return cell;
}

#pragma mark- Table View delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    playScreen.currentSongIndex = [indexPath row];
    playScreen.isPushFromSelectedRowEvent = YES;
    [self.navigationController pushViewController:playScreen animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == [songsArr count])
    {
        return TABLE_ROW_HEIGHT/2;;
    }
    return TABLE_ROW_HEIGHT;
}

#pragma mark- Actions
- (NSMutableArray *)getSongsArrByCategoryId:(int)categoryId
{
    NSMutableArray *tempSongsArr = [[NSMutableArray alloc] init];
    Song *tempSong;
    
    tempSong = [[Song alloc] initWithName:@"Em Tin" songUrl:@"nguoitinh"];
    [tempSongsArr addObject:tempSong];
    [tempSong release];
    
    tempSong = [[Song alloc] initWithName:@"Gió Đông Ấm Áp" songUrl:@"nguoitinh"];
    [tempSongsArr addObject:tempSong];
    [tempSong release];
    
    tempSong = [[Song alloc] initWithName:@"Tìm Lại Ánh Dương" songUrl:@"nguoitinh"];
    [tempSongsArr addObject:tempSong];
    [tempSong release];
    
    tempSong = [[Song alloc] initWithName:@"Chia Tay Bất Ngờ" songUrl:@"chilagiacmo"];
    [tempSongsArr addObject:tempSong];
    [tempSong release];
    
    tempSong = [[Song alloc] initWithName:@"Khi Em Trong Vòng Tay Anh" songUrl:@"nguoitinh"];
    [tempSongsArr addObject:tempSong];
    [tempSong release];
    
    tempSong = [[Song alloc] initWithName:@"Tôn Thờ Một Tình Yêu 2" songUrl:@"nguoitinh"];
    [tempSongsArr addObject:tempSong];
    [tempSong release];
    
    tempSong = [[Song alloc] initWithName:@"Quên Đi Thói Quen" songUrl:@"nguoitinh"];
    [tempSongsArr addObject:tempSong];
    [tempSong release];
    
    tempSong = [[Song alloc] initWithName:@"Níu Kéo" songUrl:@"nguoitinh"];
    [tempSongsArr addObject:tempSong];
    [tempSong release];
    
    return tempSongsArr;
}

-(void)onPlayButtonTouched:(UIButton *)button
{
    
}

@end
